#include <iostream>
#include <sw/redis++/redis.h>
#include <vector>
#include <sw/redis++/redis++.h>
#include <string>
#include <unordered_map>
#include "util.hpp"

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;
using sw::redis::Redis;

void test(Redis &redis)
{
    cout << "lpsuh and rpush and lrange" << endl;
    redis.flushall();

    redis.lpush("key", "111");

    redis.rpush("key", {"222", "333", "666"});

    vector<string> pushElem = {
        "555", "444", "777"};

    redis.lpush("key", pushElem.begin(), pushElem.end());

    vector<string> result;
    auto it = std::back_inserter(result);
    redis.lrange("key", 0, -1, it);

    printContiner(result);
}

void test2(Redis &redis)
{
    cout << "lpop and rpop" << endl;
    redis.flushall();

    redis.lpush("key", "111");
    redis.rpush("key", {"222", "333", "666"});
    vector<string> pushElem = {
        "555", "444", "777"};
    redis.lpush("key", pushElem.begin(), pushElem.end());

    vector<string> result;
    auto it = std::back_inserter(result);
    redis.lrange("key", 0, -1, it);
    printContiner(result);

    redis.lpop("key");
    redis.rpop("key");

    vector<string> eee;
    auto ite = std::back_inserter(eee);
    redis.lrange("key", 0, -1, ite);
    cout << endl;
    printContiner(eee);
}

void test3(Redis &redis)
{
    using namespace std::chrono_literals;
    cout << "blpop" << endl;
    redis.flushall();

    auto result = redis.blpop({"key1", "key2", "key3"}, 10s);
    if (result)
    {
        // cout <<"key: " << result.value().first << endl;
        // cout <<"elem: " << result.value().second << endl;
        cout << "key: " << result->first << endl;
        cout << "elem: " << result->second << endl;
    }
    else
    {
        cout << "无效元素" << endl;
    }
}

void test4(Redis &redis)
{
    using namespace std::chrono_literals;
    cout << "llen" << endl;
    redis.flushall();
    redis.lpush("key", {"222", "333", "666"});

    long long lenth = redis.llen("key");
    cout << lenth << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test2(redis);
    // test3(redis);
    test4(redis);
    return 0;
}